#!/bin/bash

WORK_PATH="/opt/middleware/AtlasEdge"
SCRIPT_PATH="/opt/middleware/AtlasEdge/software/edge_site/edge_manager/src/script"

. ${SCRIPT_PATH}/log_util.sh
. ${SCRIPT_PATH}/common.sh

print_helps()
{
    echo "atlasedge run entry cmd:"
    ${SCRIPT_PATH}/config.sh -h
    ${SCRIPT_PATH}/start.sh -h
    ${SCRIPT_PATH}/stop.sh -h
    ${SCRIPT_PATH}/upgrade.sh -h
    ${SCRIPT_PATH}/uninstall.sh -h
}

main(){

    if [ "$#" -gt 0 ]; then
        method=$1
        shell_file=""
        case "$method" in
            "config")
                shell_file="config.sh"
            ;;
            "start")
                shell_file="start.sh"
            ;;
            "stop")
                shell_file="stop.sh"
            ;;
            "upgrade")
                shell_file="upgrade.sh"
            ;;
            "uninstall")
                shell_file="uninstall.sh"            
            ;;
            "uninstallIEF")
                shell_file="uninstallIEF.sh"
            ;;
            * )
                print_helps "$@"
                exit 1
            ;;
        esac

        shift
        ${SCRIPT_PATH}/${shell_file} "$@"
        ret=$?
        if [ ${ret} != 0 ];then
#            logger_Warn "exec failed ret:" ${ret}
            if [ "$shell_file" == "uninstall.sh" ];then
                logger_Info "call uninstall sh failed force rm work dir"
                rm -rf "${WORK_PATH}"
            fi
            return $ret
        fi

    else
    print_helps "$@"
    fi
}

main "$@"
RESULT=$?
echo "ret:$RESULT"

exit $RESULT